#include "MainHeader.h"

BOOL CALLBACK  ExtendedProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
	char buf[256];
	RHeader* RoomHeader=GameEngine->Members.RoomHeader;
    u8 X=0;
	u8 Y=0;
	u8 Width=0;
	u8 Height=0;
    int i =0;
	u32 copysize=0;
    u16 roombuff[0x10000]={0};
	switch (message) 
	{
	case WM_INITDIALOG:
		ExtendedOptWND=hWnd;
		LayerCombo.Clear();
	    LayerCombo.Init(GetDlgItem(hWnd,cboLayer));
		LayerCombo.Additem("Foreground");
		LayerCombo.Additem("Level Data");
		LayerCombo.Additem("Back Layer");
		LayerCombo.Additem("Clip Data");
		LayerCombo.SetListIndex(0);
		break;
		

	case WM_LBUTTONDOWN:
		break;

		case WM_COMMAND:
			switch(LOWORD(wParam)){
			case cmdROSave:
			  MessageBox(hWnd,"Automatically Saving the Room and applying effects","Warning",MB_OK);
			  
			      GetWindowText(GetDlgItem(hWnd,txtWidth),buf,3);
				  sscanf(buf,"%x",&X);
				  GetWindowText(GetDlgItem(hWnd,txtHeight),buf,3);
				  sscanf(buf,"%x",&Y);
				  //make a new copy of RoomBuff the size of the new Width and Height
                  
				  //Fill it with zeroes
				  for(i = 0;i<X*Y;i++)roombuff[i]=0;
				  i = LayerCombo.GetListIndex();
			  if(i == 0){
				
				  //copy the old level 
				  if(ThisForeground->X*ThisForeground->Y>= X*Y){
                   copysize=X*Y;
				  }else{
                   copysize=ThisForeground->X*ThisForeground->Y;
				  }   
				  memcpy((u8*)ThisForeground->TileBuf2D,roombuff,copysize);
                  ThisForeground->X=X;
				  ThisForeground->Y=Y;
				  memset((u8*)ThisForeground->TileBuf2D,0,0x20000);
				  memcpy(roombuff,(u8*)ThisForeground->TileBuf2D,copysize);
				 
				  SaveLevel(GBA.ROM,-1);
				  return 0;
			  }else if(i == 1){
				  if(ThisLevelData->X*ThisLevelData->Y> X*Y){
                   copysize=X*Y;
				  }else{
                   copysize=ThisLevelData->X*ThisLevelData->Y;
				  }   
				  memcpy((u8*)ThisLevelData->TileBuf2D,roombuff,copysize);
                  ThisLevelData->X=X;
				  ThisLevelData->Y=Y;
				  memset((u8*)ThisLevelData->TileBuf2D,0,0x20000);
				  memcpy(roombuff,(u8*)ThisLevelData->TileBuf2D,copysize);
				 
				  SaveLevel(GBA.ROM,-1);
				  return 0;
			  }else if(i == 2){
				  if(ThisBackLayer->X*ThisBackLayer->Y>= X*Y){
                   copysize=X*Y;
				  }else{
                   copysize=ThisBackLayer->X*ThisBackLayer->Y;
				  }   
				  memcpy((u8*)ThisBackLayer->TileBuf2D,roombuff,copysize);
                  ThisBackLayer->X=X;
				  ThisBackLayer->Y=Y;
				  memset((u8*)ThisBackLayer->TileBuf2D,0,0x20000);
				  memcpy(roombuff,(u8*)ThisBackLayer->TileBuf2D,copysize);
				  
				  SaveLevel(GBA.ROM,-1);
				  return 0;
			  }else if(i == 3){
				  if(ThisClipData->X*ThisClipData->Y>= X*Y){
                   copysize=X*Y;
				  }else{
                   copysize=ThisClipData->X*ThisClipData->Y;
				  }   
				  memcpy((u8*)ThisClipData->TileBuf2D,roombuff,copysize);
                  ThisClipData->X=X;
				  ThisClipData->Y=Y;
				  memset((u8*)ThisClipData->TileBuf2D,0,0x20000);
				  memcpy(roombuff,(u8*)ThisClipData->TileBuf2D,copysize);
				  
				  SaveLevel(GBA.ROM,-1);
				  return 0;
			  }
			//Safety Measure
			  
				  break; 
			case cboLayer:
				if(HIWORD(wParam)==CBN_SELCHANGE){
                 i = LayerCombo.GetListIndex();
				 if(i == 0){//Foreground
					 if(RoomHeader->bBg0==0x10){
                        sprintf(buf,"%X",ThisForeground->X);
						SetWindowText(GetDlgItem(hWnd,txtWidth),buf);
						sprintf(buf,"%X",ThisForeground->Y);
						SetWindowText(GetDlgItem(hWnd,txtHeight),buf);
					 }else{
						 LayerCombo.SetListIndex(1);
						 SendMessage(hWnd,WM_COMMAND,wParam,lParam);
					 }

				 }else if(i == 1){//Level Data

                     if(RoomHeader->bBg1==0x10){
                        sprintf(buf,"%X",ThisLevelData->X);
						SetWindowText(GetDlgItem(hWnd,txtWidth),buf);
						sprintf(buf,"%X",ThisLevelData->Y);
						SetWindowText(GetDlgItem(hWnd,txtHeight),buf);
					 }else{
						 LayerCombo.SetListIndex(2);
						 SendMessage(hWnd,WM_COMMAND,wParam,lParam);
					 }
				 }else if(i == 2){//Back Layer
                      if(RoomHeader->bBg2==0x10){
                        sprintf(buf,"%X",ThisBackLayer->X);
						SetWindowText(GetDlgItem(hWnd,txtWidth),buf);
						sprintf(buf,"%X",ThisBackLayer->Y);
						SetWindowText(GetDlgItem(hWnd,txtHeight),buf);
					 }else{
						 LayerCombo.SetListIndex(3);
						 SendMessage(hWnd,WM_COMMAND,wParam,lParam);
					 }
				 }else if(i == 3){//Clip Data
                     sprintf(buf,"%X",ThisClipData->X);
					 SetWindowText(GetDlgItem(hWnd,txtWidth),buf);
					 sprintf(buf,"%X",ThisClipData->Y);
					 SetWindowText(GetDlgItem(hWnd,txtHeight),buf);
				 }
				
			}
				break;
				
			case cmdCFore:
			
				for(i = 0;i<ThisForeground->X* ThisForeground->Y;i++)
                     ThisForeground->TileBuf2D[i]=0x00;
				ThisForeground->Dirty =1;
				ThisForeground->SDirty =1;
					DrawRoom();
				break;
			case cmdCLevel:
			
					for(i = 0;i<ThisLevelData->X* ThisLevelData->Y;i++)
                     ThisLevelData->TileBuf2D[i]=0x00;
					ThisLevelData->SDirty =	ThisLevelData->Dirty =1 ;
				DrawRoom();
				break;
			case cmdCBack:
			
					for(i = 0;i<ThisBackLayer->X* ThisBackLayer->Y;i++)
                     ThisBackLayer->TileBuf2D[i]=0x00;
					ThisBackLayer->SDirty=ThisBackLayer->Dirty=1;
					DrawRoom();
				break;
			case cmdCClip:
			
					for(i = 0;i<ThisClipData->X* ThisClipData->Y;i++)
                     ThisClipData->TileBuf2D[i]=0x00;
					ThisClipData->SDirty=1;
				break;
		

			case IDCANCEL:
					ShowWindow(hWnd,0);
					break;
			}
			

			break;
			
			if((ThisBackLayer->Dirty )||(ThisLevelData->Dirty )||(ThisForeground->Dirty))  
				DrawRoom();
 			
		
		   
				
	}
	
	//	return DefWindowProc(hWnd, message, wParam, lParam);
	return 0;
}

int LoadExtensions()
{

	//Enable All
	//	SendMessage(GetDlgItem( ExtendedOptWND,cmdCFore),WM_ENABLE
	RHeader* RoomHeader=GameEngine->Members.RoomHeader;
	if(RoomHeader->bBg0){
		EnableWindow(GetDlgItem(ExtendedOptWND,cmdCFore),1);

	}else{
		EnableWindow(GetDlgItem(ExtendedOptWND,cmdCFore),0); 
	}
	if(RoomHeader->bBg1){
		EnableWindow(GetDlgItem(ExtendedOptWND,cmdCLevel),1); 
	}else{
		EnableWindow(GetDlgItem(ExtendedOptWND,cmdCLevel),0); 
	}
	if(RoomHeader->bBg2){
		EnableWindow(GetDlgItem(ExtendedOptWND,cmdCBack ),1); 
	}else{
		EnableWindow(GetDlgItem(ExtendedOptWND,cmdCBack ),0); 
	}


	//if(RoomHeader->bBg3)
	
	return 0;
}


